#include <cmath>

#include <control.h>
#include <cursor.h>

extern Cursor cursor;

inline bool
Control::over (double middle)
{
	return cursor.x > x - middle &&
				 cursor.x < x + middle &&
				 cursor.y > y - middle &&
				 cursor.y < y + middle;
}

static double middle;

void
Control::draw (CairoContext *context, Shape shape)
{
	middle = size / 2.0;

	if (pressed)
	{
		move ();
	}

	if (over (middle))
	{
		cairo_set_source_rgb (context, 0.0, 0.0, 1.0);
	
		if (pressed = cursor.pressed)
		{
			cairo_set_source_rgb (context, 1.0, 0.0, 0.0);
		}
	}
	else
	{
		cairo_set_source_rgb (context, 0.0, 1.0, 0.0);
	}

	switch (shape)
	{
		case BOX:
			cairo_rectangle (context, x - middle, y - middle, size, size);
			break;

		case CIRCLE:
			cairo_arc (context, x, y, middle, 0.0, 2.0 * M_PI);
			break;
	}

	cairo_fill (context);
}
